
package com.probiz.estoresf.customer.web.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.customer.Address;
import com.probiz.estore.core.controller.GenericCustomerFrontController;
import com.probiz.estore.core.exception.ApplicationException;
import com.probiz.estore.customer.service.AddressManager;
import com.probiz.estore.webapp.util.RequestContext;

/**
 * modify by huang wenmin 2009-01-20
 * @author huangwm210
 *
 */
public class AddressFrontController extends GenericCustomerFrontController<Address> {
	private AddressManager		addressManager	= null;

	public void setAddressManager(AddressManager manager) {
		this.addressManager = manager;
	}
	
	@Override
	protected String getEntityName(Address entity) {
		return null;
	}

	@Override
	protected void preSave(HttpServletRequest request, Address entity, BindException errors) {
	}

	@Override
	protected Map getMultiSaveModel(HttpServletRequest request) {
		return null;
	}

	@Override
	protected void initController() throws Exception {
		super.mgr = addressManager;
	}

	public ModelAndView defaultAction(HttpServletRequest request, HttpServletResponse response) {
		List<Address> addressList = addressManager.getAllByAppuserId(RequestContext.getCurrentUserId());

		return new ModelAndView("customer/csAddressList", "addressList", addressList);
	}
	
	public ModelAndView add(HttpServletRequest request,	HttpServletResponse response)
	throws Exception {
		ModelAndView mv = super.add(request, response);
		mv.setViewName("customer/csAddressForm");
		
		return mv;
	}
	
	public ModelAndView edit(HttpServletRequest request,	HttpServletResponse response)
	throws Exception {
		Address address = this.formBackingObject(request);
		if(null == address || !RequestContext.getCurrentUserId().equals(address.getAppuserId())){
			//当前地址不是该用户的
			try {
				response.sendError(404);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		request.setAttribute("address", address);
		ModelAndView mv = new ModelAndView("customer/csAddressForm");
		
		return mv;
	}

	public ModelAndView multiDelete(HttpServletRequest request, HttpServletResponse response) {
		String[] pkeys = request.getParameterValues("addressId");
		if (pkeys != null) {
			for (int i = 0; i < pkeys.length; i++) {
				if (pkeys[i] != null && !pkeys[i].equals("")) {
					addressManager.deleteById(new Integer(pkeys[i]));
					//save message
					String message = getMessage("address.deleted", new Object[] { new Integer(pkeys[i]) });
					saveMessage(request, message);
				}
			}
		}
		return new ModelAndView(new RedirectView("customer/csAddressList.htm"));
	}
	
    public ModelAndView save(HttpServletRequest request, HttpServletResponse response)
	throws Exception {

    	Address address = formBackingObject(request);
		address.setAddressType(Constants.ADDRESS_TYPE_SHIPPING);
		request.setAttribute("address", address);
    	BindException errors = null;
		try {
			ServletRequestDataBinder binder = bindAndValidate(request, address);
			errors = new BindException(binder.getBindingResult());
			preSave(request, address, errors);
			if (!errors.hasErrors()) {
				boolean isNew = (address.getAddressId() == null);
				addressManager.saveFully(address);

				String key = (isNew) ? "address.added" : "address.updated";
				String message = getMessage(key, new Object[] { address.getAddress() });
				saveMessage(request, message);
				
			}
		} catch (ApplicationException e) {
			handleApplicationException(errors, e);
		}
		
		ModelAndView mv = new ModelAndView("customer/csAddressForm");
		if (errors.hasErrors()) {
			mv = new ModelAndView("customer/csAddressForm", errors.getModel());
		}

		return mv;
	}

	protected Address formBackingObject(HttpServletRequest request) {
		String addressId = request.getParameter("addressId");
		Address address = null;

		if (!"".equals(request.getParameter("version")) && !StringUtils.isEmpty(addressId)) {
			try {
				Integer pkey = (new Integer(addressId));
				address = addressManager.getById(pkey);
			} catch (Exception ex) {
				ex.printStackTrace();
				address = new Address();
			}
		} else {
			address = new Address();
			address.setAppuserId(RequestContext.getCurrentUserId());
		}

		request.setAttribute("address", address);
		return address;
	}

}
